package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.Set;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllFoodtypes", query = "select myFoodtype from Foodtype myFoodtype"),
		@NamedQuery(name = "findFoodtypeByName", query = "select myFoodtype from Foodtype myFoodtype where myFoodtype.name = ?1"),
		@NamedQuery(name = "findFoodtypeByNameContaining", query = "select myFoodtype from Foodtype myFoodtype where myFoodtype.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findFoodtypeByDescription", query = "select myFoodtype from Foodtype myFoodtype where myFoodtype.description = ?1"),
		@NamedQuery(name = "findFoodtypeByDescriptionContaining", query = "select myFoodtype from Foodtype myFoodtype where myFoodtype.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllFoodtypesByParent", query = "select myFoodtype from Foodtype myFoodtype where myFoodtype.parent.id = ?1"),
		@NamedQuery(name = "findFoodtypeById", query = "select myFoodtype from Foodtype myFoodtype where myFoodtype.id = ?1"),
		@NamedQuery(name = "findFoodtypeByPrimaryKey", query = "select myFoodtype from Foodtype myFoodtype where myFoodtype.id = ?1 "),
		@NamedQuery(name = "deleteFoodtypesByIds", query = "delete Foodtype where id in (?1)"),
		@NamedQuery(name = "findFoodtypesByIds", query = "select myFoodtype from Foodtype myFoodtype where myFoodtype.id in (?1)"),
		@NamedQuery(name = "findFoodtypeIdsByParent", query = "select myFoodtype.id from Foodtype myFoodtype where myFoodtype.parent.id = ?1"),
		@NamedQuery(name = "findFoodtypeByPhyscialPrimaryKey", query = "select myFoodtype from Foodtype myFoodtype where  myFoodtype.name = ?1 and myFoodtype.parent.id = ?2"),
		@NamedQuery(name = "countFoodtypesByName", query = "select count(myFoodtype)  from Foodtype myFoodtype where myFoodtype.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "foodtype")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Foodtype")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Foodtype implements Serializable, Comparable<Foodtype> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "parent", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Foodtype parent;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "foodtype", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Food> foodsForFoodtype;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "parent", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodtype> foodtypesForParent;
	@JSONField(serialize = false)
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(catalog = "ema", name = "foodtype2standard", joinColumns = {
			@JoinColumn(name = "foodtype", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "standard", referencedColumnName = "id") })
	@XmlElement(name = "", namespace = "")
	Set<Standard> standardsForFoodtype;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setParent(Foodtype parent) {
		this.parent = parent;
	}

	public Foodtype getParent() {
		return parent;
	}

	public void setFoodsForFoodtype(Set<Food> foodsForFoodtype) {
		this.foodsForFoodtype = foodsForFoodtype;
	}

	public Set<Food> getFoodsForFoodtype() {
		if (foodsForFoodtype == null) {
			foodsForFoodtype = new java.util.LinkedHashSet<Food>();
		}
		return foodsForFoodtype;
	}

	public void setFoodtypesForParent(Set<Foodtype> foodtypesForParent) {
		this.foodtypesForParent = foodtypesForParent;
	}

	public Set<Foodtype> getFoodtypesForParent() {
		if (foodtypesForParent == null) {
			foodtypesForParent = new java.util.LinkedHashSet<Foodtype>();
		}
		return foodtypesForParent;
	}

	public void setStandardsForFoodtype(Set<Standard> standardsForFoodtype) {
		this.standardsForFoodtype = standardsForFoodtype;
	}

	public Set<Standard> getStandardsForFoodtype() {
		if (standardsForFoodtype == null) {
			standardsForFoodtype = new java.util.LinkedHashSet<Standard>();
		}
		return standardsForFoodtype;
	}

	public void copy(Foodtype that) {

		setId(that.getId());

		setName(that.getName());
		setDescription(that.getDescription());

		setParent(that.getParent());

		setFoodsForFoodtype(new java.util.LinkedHashSet<Food>(that.getFoodsForFoodtype()));

		setFoodtypesForParent(
				new java.util.LinkedHashSet<Foodtype>(that.getFoodtypesForParent()));

		setStandardsForFoodtype(
				new java.util.LinkedHashSet<Standard>(that.getStandardsForFoodtype()));

	}

	public Foodtype() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Foodtype))
			return false;
		Foodtype comparedFoodtype = (Foodtype) obj;
		if (!CheckPropertyEqual(this.name, comparedFoodtype.name)) {
			return false;
		}
		if (!CheckPropertyEqual(this.parent.id, comparedFoodtype.parent.id)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Foodtype o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
